package model;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import dal.*;
import util.*;

public class PurchaseModel implements IModel {

	public static class FieldNames {
		public static String Id = "Id";
		public static String Name = "Name";
		public static String Code = "Code";
		public static String Description = "Description";
		public static String PlanCost = "PlanCost";
		public static String RealCost = "RealCost";
		public static String PlanOrderTime = "PlanOrderTime";
		public static String PlanDeliveryTime = "PlanDeliveryTime";
		public static String RealOrderTate = "RealOrderTate";
		public static String RealDeliveryTime = "RealDeliveryTime";
		public static String NeedApproval = "NeedApproval";
		public static String Status = "Status";
		public static String IsDraft = "IsDraft";
		public static String Notification = "Notification";
		public static String Created = "Created";
		public static String Creator = "Creator";
		public static String Updated = "Updated";
		public static String Updator = "Updator";
		public static String Deleted = "Deleted";
		public static String DeletionTime = "DeletionTime";
		public static String Remark = "Remark";
		public static String Purchaser = "Purchaser";
		public static String Supplier = "Supplier";
		public static String IdCurrency = "IdCurrency";
		public static String IdProject = "IdProject";
		public static String Discount = "Discount";
		public static String AdditionalCost = "AdditionalCost";
		public static String OrderTime = "OrderTime";
		public static String IdQuotation = "IdQuotation";
		public static String IdQuotationItem = "IdQuotationItem";
		public static String IdChecklist = "IdChecklist";
		public static String IdTask = "IdTask";
		public static String IdEvent = "IdEvent";
		public static String PaymentDate = "PaymentDate";
	
	}

	public class ErrorMessages {
		public String Id = "";
		public String Name = "";
		public String Code = "";
		public String Description = "";
		public String PlanCost = "";
		public String RealCost = "";
		public String PlanOrderTime = "";
		public String PlanDeliveryTime = "";
		public String RealOrderTate = "";
		public String RealDeliveryTime = "";
		public String NeedApproval = "";
		public String Status = "";
		public String IsDraft = "";
		public String Notification = "";
		public String Created = "";
		public String Creator = "";
		public String Updated = "";
		public String Updator = "";
		public String Deleted = "";
		public String DeletionTime = "";
		public String Remark = "";
		public String Purchaser = "";
		public String Supplier = "";
		public String IdCurrency = "";
		public String IdProject = "";
		public String Discount = "";
		public String AdditionalCost = "";
		public String OrderTime = "";
		public String IdQuotation = "";
		public String IdQuotationItem = "";
		public String IdChecklist = "";
		public String IdTask = "";
		public String IdEvent = "";
		public String PaymentDate = "";
	
	}

    public ErrorMessages errMsgs = new ErrorMessages();

	public String[] errMsgList = null;

// private variables
private int _id = Integer.MIN_VALUE;
private String _name = null;
private String _code = null;
private String _description = null;
private double _plan_cost = Double.MAX_VALUE;
private double _real_cost = Double.MAX_VALUE;
private Date _plan_order_time = null;
private Date _plan_delivery_time = null;
private Date _real_order_tate = null;
private Date _real_delivery_time = null;
private boolean _need_approval = false;
private boolean _isneed_approvalSet = false;
private int _status = Integer.MIN_VALUE;
private boolean _is_draft = false;
private boolean _isis_draftSet = false;
private int _notification = Integer.MIN_VALUE;
private Date _created = null;
private int _creator = Integer.MIN_VALUE;
private Date _updated = null;
private int _updator = Integer.MIN_VALUE;
private boolean _deleted = false;
private boolean _isdeletedSet = false;
private Date _deletion_time = null;
private String _remark = null;
private int _purchaser = Integer.MIN_VALUE;
private int _supplier = Integer.MIN_VALUE;
private int _id_currency = Integer.MIN_VALUE;
private int _id_project = Integer.MIN_VALUE;
private double _discount = Double.MAX_VALUE;
private double _additional_cost = Double.MAX_VALUE;
private Date _order_time = null;
private int _id_quotation = Integer.MIN_VALUE;
private int _id_quotation_item = Integer.MIN_VALUE;
private int _id_checklist = Integer.MIN_VALUE;
private int _id_task = Integer.MIN_VALUE;
private int _id_event = Integer.MIN_VALUE;
private Date _payment_date = null;
private double _purchase_cost = Double.MAX_VALUE; // Dat added
private double _sum_item_cost = Double.MAX_VALUE; // Dat added

	public PurchaseModel() {
	}

// get/set
public int getId() {
		return _id;
	}
public void setId(int value) {
		_id = value;
	}
public String getIdToString() {
		if (_id == Integer.MIN_VALUE
				|| _id == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id);
		}
	}
public String getName() {
		return _name;
	}
public void setName(String value) {
		_name = value;
	}
public String getNameToString() {
		if (_name != null) {
			return HtmlUtil.genHtml(_name);
		} else {
			return "";
		}
	}
public String getCode() {
		return _code;
	}
public void setCode(String value) {
		_code = value;
	}
public String getCodeToString() {
		if (_code != null) {
			return HtmlUtil.genHtml(_code);
		} else {
			return "";
		}
	}
public String getDescription() {
		return _description;
	}
public void setDescription(String value) {
		_description = value;
	}
public String getDescriptionToString() {
		if (_description != null) {
			return HtmlUtil.genHtml(_description);
		} else {
			return "";
		}
	}
public double getPlanCost() {
		return _plan_cost;
	}
public void setPlanCost(double value) {
		_plan_cost = value;
	}
	public String getPlanCostToString() {
		if (_plan_cost == Double.MIN_VALUE || _plan_cost == Double.MAX_VALUE) {
			return "";
		} else {
			return StringUtil.toString(_plan_cost);
		}
	}
public double getRealCost() {
		return _real_cost;
	}
public void setRealCost(double value) {
		_real_cost = value;
	}
	public String getRealCostToString() {
		if (_real_cost == Double.MIN_VALUE || _real_cost == Double.MAX_VALUE) {
			return "";
		} else {
			return StringUtil.toString(_real_cost);
		}
	}
public Date getPlanOrderTime() {
		return _plan_order_time;
	}
public void setPlanOrderTime(Date value) {
		_plan_order_time = value;
	}
public String getPlanOrderTimeToString() {
		if (_plan_order_time != null) {
			return StringUtil.toDateTimeString(_plan_order_time);
		} else {
			return "";
		}
	}
public Date getPlanDeliveryTime() {
		return _plan_delivery_time;
	}
public void setPlanDeliveryTime(Date value) {
		_plan_delivery_time = value;
	}
public String getPlanDeliveryTimeToString() {
		if (_plan_delivery_time != null) {
			return StringUtil.toDateTimeString(_plan_delivery_time);
		} else {
			return "";
		}
	}
public Date getRealOrderTate() {
		return _real_order_tate;
	}
public void setRealOrderTate(Date value) {
		_real_order_tate = value;
	}
public String getRealOrderTateToString() {
		if (_real_order_tate != null) {
			return StringUtil.toDateTimeString(_real_order_tate);
		} else {
			return "";
		}
	}
public Date getRealDeliveryTime() {
		return _real_delivery_time;
	}
public void setRealDeliveryTime(Date value) {
		_real_delivery_time = value;
	}
public String getRealDeliveryTimeToString() {
		if (_real_delivery_time != null) {
			return StringUtil.toDateTimeString(_real_delivery_time);
		} else {
			return "";
		}
	}
public boolean getNeedApproval() {
		return _need_approval;
	}
public void setNeedApproval(boolean value) {
		_need_approval = value;
		_isneed_approvalSet = true;
	}
public boolean getNeedApprovalSet() {
		return _isneed_approvalSet;
	}
	public String getNeedApprovalToString() {
		if (_isneed_approvalSet) {
			return HtmlUtil.genHtml(_need_approval);
		} else {
			return "";
		}
	}
public int getStatus() {
		return _status;
	}
public void setStatus(int value) {
		_status = value;
	}
public String getStatusToString() {
		if (_status == Integer.MIN_VALUE
				|| _status == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_status);
		}
	}
public boolean getIsDraft() {
		return _is_draft;
	}
public void setIsDraft(boolean value) {
		_is_draft = value;
		_isis_draftSet = true;
	}
public boolean getIsDraftSet() {
		return _isis_draftSet;
	}
	public String getIsDraftToString() {
		if (_isis_draftSet) {
			return HtmlUtil.genHtml(_is_draft);
		} else {
			return "";
		}
	}
public int getNotification() {
		return _notification;
	}
public void setNotification(int value) {
		_notification = value;
	}
public String getNotificationToString() {
		if (_notification == Integer.MIN_VALUE
				|| _notification == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_notification);
		}
	}
public Date getCreated() {
		return _created;
	}
public void setCreated(Date value) {
		_created = value;
	}
public String getCreatedToString() {
		if (_created != null) {
			return StringUtil.toDateTimeString(_created);
		} else {
			return "";
		}
	}
public int getCreator() {
		return _creator;
	}
public void setCreator(int value) {
		_creator = value;
	}
public String getCreatorToString() {
		if (_creator == Integer.MIN_VALUE
				|| _creator == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_creator);
		}
	}
public Date getUpdated() {
		return _updated;
	}
public void setUpdated(Date value) {
		_updated = value;
	}
public String getUpdatedToString() {
		if (_updated != null) {
			return StringUtil.toDateTimeString(_updated);
		} else {
			return "";
		}
	}
public int getUpdator() {
		return _updator;
	}
public void setUpdator(int value) {
		_updator = value;
	}
public String getUpdatorToString() {
		if (_updator == Integer.MIN_VALUE
				|| _updator == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_updator);
		}
	}
public boolean getDeleted() {
		return _deleted;
	}
public void setDeleted(boolean value) {
		_deleted = value;
		_isdeletedSet = true;
	}
public boolean getDeletedSet() {
		return _isdeletedSet;
	}
	public String getDeletedToString() {
		if (_isdeletedSet) {
			return HtmlUtil.genHtml(_deleted);
		} else {
			return "";
		}
	}
public Date getDeletionTime() {
		return _deletion_time;
	}
public void setDeletionTime(Date value) {
		_deletion_time = value;
	}
public String getDeletionTimeToString() {
		if (_deletion_time != null) {
			return StringUtil.toDateTimeString(_deletion_time);
		} else {
			return "";
		}
	}
public String getRemark() {
		return _remark;
	}
public void setRemark(String value) {
		_remark = value;
	}
public String getRemarkToString() {
		if (_remark != null) {
			return HtmlUtil.genHtml(_remark);
		} else {
			return "";
		}
	}
public int getPurchaser() {
		return _purchaser;
	}
public void setPurchaser(int value) {
		_purchaser = value;
	}
public String getPurchaserToString() {
		if (_purchaser == Integer.MIN_VALUE
				|| _purchaser == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_purchaser);
		}
	}
public int getSupplier() {
		return _supplier;
	}
public void setSupplier(int value) {
		_supplier = value;
	}
public String getSupplierToString() {
		if (_supplier == Integer.MIN_VALUE
				|| _supplier == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_supplier);
		}
	}
public int getIdCurrency() {
		return _id_currency;
	}
public void setIdCurrency(int value) {
		_id_currency = value;
	}
public String getIdCurrencyToString() {
		if (_id_currency == Integer.MIN_VALUE
				|| _id_currency == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_currency);
		}
	}
public int getIdProject() {
		return _id_project;
	}
public void setIdProject(int value) {
		_id_project = value;
	}
public String getIdProjectToString() {
		if (_id_project == Integer.MIN_VALUE
				|| _id_project == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_project);
		}
	}
public double getDiscount() {
		return _discount;
	}
public void setDiscount(double value) {
		_discount = value;
	}
	public String getDiscountToString() {
		if (_discount == Double.MIN_VALUE || _discount == Double.MAX_VALUE) {
			return "";
		} else {
			return StringUtil.toString(_discount);
		}
	}
public double getAdditionalCost() {
		return _additional_cost;
	}
public void setAdditionalCost(double value) {
		_additional_cost = value;
	}
	public String getAdditionalCostToString() {
		if (_additional_cost == Double.MIN_VALUE || _additional_cost == Double.MAX_VALUE) {
			return "";
		} else {
			return StringUtil.toString(_additional_cost);
		}
	}
public Date getOrderTime() {
		return _order_time;
	}
public void setOrderTime(Date value) {
		_order_time = value;
	}
public String getOrderTimeToString() {
		if (_order_time != null) {
			return StringUtil.toDateTimeString(_order_time);
		} else {
			return "";
		}
	}
public int getIdQuotation() {
		return _id_quotation;
	}
public void setIdQuotation(int value) {
		_id_quotation = value;
	}
public String getIdQuotationToString() {
		if (_id_quotation == Integer.MIN_VALUE
				|| _id_quotation == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_quotation);
		}
	}
public int getIdQuotationItem() {
		return _id_quotation_item;
	}
public void setIdQuotationItem(int value) {
		_id_quotation_item = value;
	}
public String getIdQuotationItemToString() {
		if (_id_quotation_item == Integer.MIN_VALUE
				|| _id_quotation_item == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_quotation_item);
		}
	}
public int getIdChecklist() {
		return _id_checklist;
	}
public void setIdChecklist(int value) {
		_id_checklist = value;
	}
public String getIdChecklistToString() {
		if (_id_checklist == Integer.MIN_VALUE
				|| _id_checklist == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_checklist);
		}
	}
public int getIdTask() {
		return _id_task;
	}
public void setIdTask(int value) {
		_id_task = value;
	}
public String getIdTaskToString() {
		if (_id_task == Integer.MIN_VALUE
				|| _id_task == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_task);
		}
	}
public int getIdEvent() {
		return _id_event;
	}
public void setIdEvent(int value) {
		_id_event = value;
	}
public String getIdEventToString() {
		if (_id_event == Integer.MIN_VALUE
				|| _id_event == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id_event);
		}
	}
public Date getPaymentDate() {
		return _payment_date;
	}
public void setPaymentDate(Date value) {
		_payment_date = value;
	}
public String getPaymentDateToString() {
		if (_payment_date != null) {
			return StringUtil.toDateTimeString(_payment_date);
		} else {
			return "";
		}
	}


	@Override
	public int[] getPrimaryKey() {
		int[] ids = new int[1];
		ids[0] = getId();
		return ids;
	}

	@Override
	public IModel readFromResultSet(ResultSet rs) throws SQLException {
		PurchaseModel model = null;

			model = new PurchaseModel();

// read fields
{
				int value = rs.getInt("id");
				if (!rs.wasNull()) {
					model.setId(value);
				}
			}
{
				String value = rs.getString("name");
				if (!rs.wasNull()) {
					model.setName(value);
				}
			}
{
				String value = rs.getString("code");
				if (!rs.wasNull()) {
					model.setCode(value);
				}
			}
{
				String value = rs.getString("description");
				if (!rs.wasNull()) {
					model.setDescription(value);
				}
			}
{
				double value = rs.getDouble("plan_cost");
				if (!rs.wasNull()) {
					model.setPlanCost(value);
				}
			}
{
				double value = rs.getDouble("real_cost");
				if (!rs.wasNull()) {
					model.setRealCost(value);
				}
			}
{
				Date value = rs.getTimestamp("plan_order_time");
				if (!rs.wasNull()) {
					model.setPlanOrderTime(value);
				}
			}
{
				Date value = rs.getTimestamp("plan_delivery_time");
				if (!rs.wasNull()) {
					model.setPlanDeliveryTime(value);
				}
			}
{
				Date value = rs.getTimestamp("real_order_tate");
				if (!rs.wasNull()) {
					model.setRealOrderTate(value);
				}
			}
{
				Date value = rs.getTimestamp("real_delivery_time");
				if (!rs.wasNull()) {
					model.setRealDeliveryTime(value);
				}
			}
{
				boolean value = rs.getBoolean("need_approval");
				if (!rs.wasNull()) {
					model.setNeedApproval(value);
				}
			}
{
				int value = rs.getInt("status");
				if (!rs.wasNull()) {
					model.setStatus(value);
				}
			}
{
				boolean value = rs.getBoolean("is_draft");
				if (!rs.wasNull()) {
					model.setIsDraft(value);
				}
			}
{
				int value = rs.getInt("notification");
				if (!rs.wasNull()) {
					model.setNotification(value);
				}
			}
{
				Date value = rs.getTimestamp("created");
				if (!rs.wasNull()) {
					model.setCreated(value);
				}
			}
{
				int value = rs.getInt("creator");
				if (!rs.wasNull()) {
					model.setCreator(value);
				}
			}
{
				Date value = rs.getTimestamp("updated");
				if (!rs.wasNull()) {
					model.setUpdated(value);
				}
			}
{
				int value = rs.getInt("updator");
				if (!rs.wasNull()) {
					model.setUpdator(value);
				}
			}
{
				boolean value = rs.getBoolean("deleted");
				if (!rs.wasNull()) {
					model.setDeleted(value);
				}
			}
{
				Date value = rs.getTimestamp("deletion_time");
				if (!rs.wasNull()) {
					model.setDeletionTime(value);
				}
			}
{
				String value = rs.getString("remark");
				if (!rs.wasNull()) {
					model.setRemark(value);
				}
			}
{
				int value = rs.getInt("purchaser");
				if (!rs.wasNull()) {
					model.setPurchaser(value);
				}
			}
{
				int value = rs.getInt("supplier");
				if (!rs.wasNull()) {
					model.setSupplier(value);
				}
			}
{
				int value = rs.getInt("id_currency");
				if (!rs.wasNull()) {
					model.setIdCurrency(value);
				}
			}
{
				int value = rs.getInt("id_project");
				if (!rs.wasNull()) {
					model.setIdProject(value);
				}
			}
{
				double value = rs.getDouble("discount");
				if (!rs.wasNull()) {
					model.setDiscount(value);
				}
			}
{
				double value = rs.getDouble("additional_cost");
				if (!rs.wasNull()) {
					model.setAdditionalCost(value);
				}
			}
{
				Date value = rs.getTimestamp("order_time");
				if (!rs.wasNull()) {
					model.setOrderTime(value);
				}
			}
{
				int value = rs.getInt("id_quotation");
				if (!rs.wasNull()) {
					model.setIdQuotation(value);
				}
			}
{
				int value = rs.getInt("id_quotation_item");
				if (!rs.wasNull()) {
					model.setIdQuotationItem(value);
				}
			}
{
				int value = rs.getInt("id_checklist");
				if (!rs.wasNull()) {
					model.setIdChecklist(value);
				}
			}
{
				int value = rs.getInt("id_task");
				if (!rs.wasNull()) {
					model.setIdTask(value);
				}
			}
{
				int value = rs.getInt("id_event");
				if (!rs.wasNull()) {
					model.setIdEvent(value);
				}
			}
{
				Date value = rs.getTimestamp("payment_date");
				if (!rs.wasNull()) {
					model.setPaymentDate(value);
				}
			}
		

		return model;
	}

	@Override
	public PreparedStatement genSelectStatement(Connection dbConn, IModel query)
			throws SQLException {
		String sql = "SELECT * FROM sp_purchase_select(:id);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

        PurchaseModel obj = (PurchaseModel )query;
		
// set parameters
st.setObject("id", obj.getId());


		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genSelectListStatement(Connection dbConn,
			IModelExt query) throws SQLException {
		String sql = "SELECT * FROM sp_purchase_selectList(:limit, :offset, :id,:name,:code,:description,:plan_cost,:real_cost,:plan_order_timeFrom,:plan_order_timeTo,:plan_delivery_timeFrom,:plan_delivery_timeTo,:real_order_tateFrom,:real_order_tateTo,:real_delivery_timeFrom,:real_delivery_timeTo,:need_approval,:status,:is_draft,:notification,:createdFrom,:createdTo,:creator,:updatedFrom,:updatedTo,:updator,:deleted,:deletion_timeFrom,:deletion_timeTo,:remark,:purchaser,:supplier,:id_currency,:id_project,:discount,:additional_cost,:order_timeFrom,:order_timeTo,:id_quotation,:id_quotation_item,:id_checklist,:id_task,:id_event,:payment_dateFrom,:payment_dateTo)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		PurchaseModelExt obj = (PurchaseModelExt) query;

		if (obj.getLimit() != Integer.MIN_VALUE) {
			st.setObject("limit", obj.getLimit());
		}  else {
			st.setObject("limit", Integer.MAX_VALUE);
		}
		if (obj.getOffset() != Integer.MIN_VALUE) {
			st.setObject("offset", obj.getOffset());
		}  else {
			st.setObject("offset", 0);
		}

// set parameters
if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
            st.setObject("id", null);
}
if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
            st.setObject("name", null);
}
if (obj.getCode() != null) {
			st.setObject("code", obj.getCode());
		} else {
            st.setObject("code", null);
}
if (obj.getDescription() != null) {
			st.setObject("description", obj.getDescription());
		} else {
            st.setObject("description", null);
}
if (obj.getPlanCost() != Double.MAX_VALUE) {
			st.setObject("plan_cost", obj.getPlanCost());
		} else {
            st.setObject("plan_cost", null);
}
if (obj.getRealCost() != Double.MAX_VALUE) {
			st.setObject("real_cost", obj.getRealCost());
		} else {
            st.setObject("real_cost", null);
}
if (obj.getPlanOrderTimeFrom() != null) {
			st.setObject("plan_order_timeFrom", obj.getPlanOrderTimeFrom());
		} else {
			st.setObject("plan_order_timeFrom", null);
		}
		if (obj.getPlanOrderTimeTo() != null) {
			st.setObject("plan_order_timeTo", obj.getPlanOrderTimeTo());
		} else {
            st.setObject("plan_order_timeTo", null);
}
if (obj.getPlanDeliveryTimeFrom() != null) {
			st.setObject("plan_delivery_timeFrom", obj.getPlanDeliveryTimeFrom());
		} else {
			st.setObject("plan_delivery_timeFrom", null);
		}
		if (obj.getPlanDeliveryTimeTo() != null) {
			st.setObject("plan_delivery_timeTo", obj.getPlanDeliveryTimeTo());
		} else {
            st.setObject("plan_delivery_timeTo", null);
}
if (obj.getRealOrderTateFrom() != null) {
			st.setObject("real_order_tateFrom", obj.getRealOrderTateFrom());
		} else {
			st.setObject("real_order_tateFrom", null);
		}
		if (obj.getRealOrderTateTo() != null) {
			st.setObject("real_order_tateTo", obj.getRealOrderTateTo());
		} else {
            st.setObject("real_order_tateTo", null);
}
if (obj.getRealDeliveryTimeFrom() != null) {
			st.setObject("real_delivery_timeFrom", obj.getRealDeliveryTimeFrom());
		} else {
			st.setObject("real_delivery_timeFrom", null);
		}
		if (obj.getRealDeliveryTimeTo() != null) {
			st.setObject("real_delivery_timeTo", obj.getRealDeliveryTimeTo());
		} else {
            st.setObject("real_delivery_timeTo", null);
}
		if (obj.getNeedApprovalSet()) {
			st.setObject("need_approval", obj.getNeedApproval());
		} else {
            st.setObject("need_approval", null);
}
if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
            st.setObject("status", null);
}
		if (obj.getIsDraftSet()) {
			st.setObject("is_draft", obj.getIsDraft());
		} else {
            st.setObject("is_draft", null);
}
if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
            st.setObject("notification", null);
}
if (obj.getCreatedFrom() != null) {
			st.setObject("createdFrom", obj.getCreatedFrom());
		} else {
			st.setObject("createdFrom", null);
		}
		if (obj.getCreatedTo() != null) {
			st.setObject("createdTo", obj.getCreatedTo());
		} else {
            st.setObject("createdTo", null);
}
if (obj.getCreator() != Integer.MIN_VALUE) {
			st.setObject("creator", obj.getCreator());
		} else {
            st.setObject("creator", null);
}
if (obj.getUpdatedFrom() != null) {
			st.setObject("updatedFrom", obj.getUpdatedFrom());
		} else {
			st.setObject("updatedFrom", null);
		}
		if (obj.getUpdatedTo() != null) {
			st.setObject("updatedTo", obj.getUpdatedTo());
		} else {
            st.setObject("updatedTo", null);
}
if (obj.getUpdator() != Integer.MIN_VALUE) {
			st.setObject("updator", obj.getUpdator());
		} else {
            st.setObject("updator", null);
}
		if (obj.getDeletedSet()) {
			st.setObject("deleted", obj.getDeleted());
		} else {
            st.setObject("deleted", null);
}
if (obj.getDeletionTimeFrom() != null) {
			st.setObject("deletion_timeFrom", obj.getDeletionTimeFrom());
		} else {
			st.setObject("deletion_timeFrom", null);
		}
		if (obj.getDeletionTimeTo() != null) {
			st.setObject("deletion_timeTo", obj.getDeletionTimeTo());
		} else {
            st.setObject("deletion_timeTo", null);
}
if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
            st.setObject("remark", null);
}
if (obj.getPurchaser() != Integer.MIN_VALUE) {
			st.setObject("purchaser", obj.getPurchaser());
		} else {
            st.setObject("purchaser", null);
}
if (obj.getSupplier() != Integer.MIN_VALUE) {
			st.setObject("supplier", obj.getSupplier());
		} else {
            st.setObject("supplier", null);
}
if (obj.getIdCurrency() != Integer.MIN_VALUE) {
			st.setObject("id_currency", obj.getIdCurrency());
		} else {
            st.setObject("id_currency", null);
}
if (obj.getIdProject() != Integer.MIN_VALUE) {
			st.setObject("id_project", obj.getIdProject());
		} else {
            st.setObject("id_project", null);
}
if (obj.getDiscount() != Double.MAX_VALUE) {
			st.setObject("discount", obj.getDiscount());
		} else {
            st.setObject("discount", null);
}
if (obj.getAdditionalCost() != Double.MAX_VALUE) {
			st.setObject("additional_cost", obj.getAdditionalCost());
		} else {
            st.setObject("additional_cost", null);
}
if (obj.getOrderTimeFrom() != null) {
			st.setObject("order_timeFrom", obj.getOrderTimeFrom());
		} else {
			st.setObject("order_timeFrom", null);
		}
		if (obj.getOrderTimeTo() != null) {
			st.setObject("order_timeTo", obj.getOrderTimeTo());
		} else {
            st.setObject("order_timeTo", null);
}
if (obj.getIdQuotation() != Integer.MIN_VALUE) {
			st.setObject("id_quotation", obj.getIdQuotation());
		} else {
            st.setObject("id_quotation", null);
}
if (obj.getIdQuotationItem() != Integer.MIN_VALUE) {
			st.setObject("id_quotation_item", obj.getIdQuotationItem());
		} else {
            st.setObject("id_quotation_item", null);
}
if (obj.getIdChecklist() != Integer.MIN_VALUE) {
			st.setObject("id_checklist", obj.getIdChecklist());
		} else {
            st.setObject("id_checklist", null);
}
if (obj.getIdTask() != Integer.MIN_VALUE) {
			st.setObject("id_task", obj.getIdTask());
		} else {
            st.setObject("id_task", null);
}
if (obj.getIdEvent() != Integer.MIN_VALUE) {
			st.setObject("id_event", obj.getIdEvent());
		} else {
            st.setObject("id_event", null);
}
if (obj.getPaymentDateFrom() != null) {
			st.setObject("payment_dateFrom", obj.getPaymentDateFrom());
		} else {
			st.setObject("payment_dateFrom", null);
		}
		if (obj.getPaymentDateTo() != null) {
			st.setObject("payment_dateTo", obj.getPaymentDateTo());
		} else {
            st.setObject("payment_dateTo", null);
}
	

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genInsertStatement(Connection dbConn, IModel model)
			throws SQLException {
		String sql = "SELECT sp_purchase_insert(:name,:code,:description,:plan_cost,:real_cost,:plan_order_time,:plan_delivery_time,:real_order_tate,:real_delivery_time,:need_approval,:status,:is_draft,:notification,:creator,:remark,:purchaser,:supplier,:id_currency,:id_project,:discount,:additional_cost,:order_time,:id_quotation,:id_quotation_item,:id_checklist,:id_task,:id_event,:payment_date)";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		PurchaseModel obj = (PurchaseModel) model;

		// set parameters here
if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
}
if (obj.getCode() != null) {
			st.setObject("code", obj.getCode());
		} else {
			st.setObject("code", null);
}
if (obj.getDescription() != null) {
			st.setObject("description", obj.getDescription());
		} else {
			st.setObject("description", null);
}
if (obj.getPlanCost() != Double.MAX_VALUE) {
			st.setObject("plan_cost", obj.getPlanCost());
		} else {
			st.setObject("plan_cost", null);
}
if (obj.getRealCost() != Double.MAX_VALUE) {
			st.setObject("real_cost", obj.getRealCost());
		} else {
			st.setObject("real_cost", null);
}
if (obj.getPlanOrderTime() != null) {
			st.setObject("plan_order_time", obj.getPlanOrderTime());
		} else {
			st.setObject("plan_order_time", null);
}
if (obj.getPlanDeliveryTime() != null) {
			st.setObject("plan_delivery_time", obj.getPlanDeliveryTime());
		} else {
			st.setObject("plan_delivery_time", null);
}
if (obj.getRealOrderTate() != null) {
			st.setObject("real_order_tate", obj.getRealOrderTate());
		} else {
			st.setObject("real_order_tate", null);
}
if (obj.getRealDeliveryTime() != null) {
			st.setObject("real_delivery_time", obj.getRealDeliveryTime());
		} else {
			st.setObject("real_delivery_time", null);
}
		if (obj.getNeedApprovalSet()) {
			st.setObject("need_approval", obj.getNeedApproval());
		} else {
			st.setObject("need_approval", null);
}
if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
}
		if (obj.getIsDraftSet()) {
			st.setObject("is_draft", obj.getIsDraft());
		} else {
			st.setObject("is_draft", null);
}
if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
}
if (obj.getCreator() != Integer.MIN_VALUE) {
			st.setObject("creator", obj.getCreator());
		} else {
			st.setObject("creator", null);
}
if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
}
if (obj.getPurchaser() != Integer.MIN_VALUE) {
			st.setObject("purchaser", obj.getPurchaser());
		} else {
			st.setObject("purchaser", null);
}
if (obj.getSupplier() != Integer.MIN_VALUE) {
			st.setObject("supplier", obj.getSupplier());
		} else {
			st.setObject("supplier", null);
}
if (obj.getIdCurrency() != Integer.MIN_VALUE) {
			st.setObject("id_currency", obj.getIdCurrency());
		} else {
			st.setObject("id_currency", null);
}
if (obj.getIdProject() != Integer.MIN_VALUE) {
			st.setObject("id_project", obj.getIdProject());
		} else {
			st.setObject("id_project", null);
}
if (obj.getDiscount() != Double.MAX_VALUE) {
			st.setObject("discount", obj.getDiscount());
		} else {
			st.setObject("discount", null);
}
if (obj.getAdditionalCost() != Double.MAX_VALUE) {
			st.setObject("additional_cost", obj.getAdditionalCost());
		} else {
			st.setObject("additional_cost", null);
}
if (obj.getOrderTime() != null) {
			st.setObject("order_time", obj.getOrderTime());
		} else {
			st.setObject("order_time", null);
}
if (obj.getIdQuotation() != Integer.MIN_VALUE) {
			st.setObject("id_quotation", obj.getIdQuotation());
		} else {
			st.setObject("id_quotation", null);
}
if (obj.getIdQuotationItem() != Integer.MIN_VALUE) {
			st.setObject("id_quotation_item", obj.getIdQuotationItem());
		} else {
			st.setObject("id_quotation_item", null);
}
if (obj.getIdChecklist() != Integer.MIN_VALUE) {
			st.setObject("id_checklist", obj.getIdChecklist());
		} else {
			st.setObject("id_checklist", null);
}
if (obj.getIdTask() != Integer.MIN_VALUE) {
			st.setObject("id_task", obj.getIdTask());
		} else {
			st.setObject("id_task", null);
}
if (obj.getIdEvent() != Integer.MIN_VALUE) {
			st.setObject("id_event", obj.getIdEvent());
		} else {
			st.setObject("id_event", null);
}
if (obj.getPaymentDate() != null) {
			st.setObject("payment_date", obj.getPaymentDate());
		} else {
			st.setObject("payment_date", null);
}
	

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genUpdateStatement(Connection dbConn, IModel model)
			throws SQLException {
		String sql = "SELECT sp_purchase_update(:id,:name,:code,:description,:plan_cost,:real_cost,:plan_order_time,:plan_delivery_time,:real_order_tate,:real_delivery_time,:need_approval,:status,:is_draft,:notification,:updator,:deleted,:deletion_time,:remark,:purchaser,:supplier,:id_currency,:id_project,:discount,:additional_cost,:order_time,:id_quotation,:id_quotation_item,:id_checklist,:id_task,:id_event,:payment_date)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		PurchaseModel obj = (PurchaseModel) model;

		// set parameters here
if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
}
if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
			st.setObject("name", null);
}
if (obj.getCode() != null) {
			st.setObject("code", obj.getCode());
		} else {
			st.setObject("code", null);
}
if (obj.getDescription() != null) {
			st.setObject("description", obj.getDescription());
		} else {
			st.setObject("description", null);
}
if (obj.getPlanCost() != Double.MAX_VALUE) {
			st.setObject("plan_cost", obj.getPlanCost());
		} else {
			st.setObject("plan_cost", null);
}
if (obj.getRealCost() != Double.MAX_VALUE) {
			st.setObject("real_cost", obj.getRealCost());
		} else {
			st.setObject("real_cost", null);
}
if (obj.getPlanOrderTime() != null) {
			st.setObject("plan_order_time", obj.getPlanOrderTime());
		} else {
			st.setObject("plan_order_time", null);
}
if (obj.getPlanDeliveryTime() != null) {
			st.setObject("plan_delivery_time", obj.getPlanDeliveryTime());
		} else {
			st.setObject("plan_delivery_time", null);
}
if (obj.getRealOrderTate() != null) {
			st.setObject("real_order_tate", obj.getRealOrderTate());
		} else {
			st.setObject("real_order_tate", null);
}
if (obj.getRealDeliveryTime() != null) {
			st.setObject("real_delivery_time", obj.getRealDeliveryTime());
		} else {
			st.setObject("real_delivery_time", null);
}
		if (obj.getNeedApprovalSet()) {
			st.setObject("need_approval", obj.getNeedApproval());
		} else {
			st.setObject("need_approval", null);
}
if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
			st.setObject("status", null);
}
		if (obj.getIsDraftSet()) {
			st.setObject("is_draft", obj.getIsDraft());
		} else {
			st.setObject("is_draft", null);
}
if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
			st.setObject("notification", null);
}
if (obj.getUpdator() != Integer.MIN_VALUE) {
			st.setObject("updator", obj.getUpdator());
		} else {
			st.setObject("updator", null);
}
		if (obj.getDeletedSet()) {
			st.setObject("deleted", obj.getDeleted());
		} else {
			st.setObject("deleted", null);
}
if (obj.getDeletionTime() != null) {
			st.setObject("deletion_time", obj.getDeletionTime());
		} else {
			st.setObject("deletion_time", null);
}
if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
			st.setObject("remark", null);
}
if (obj.getPurchaser() != Integer.MIN_VALUE) {
			st.setObject("purchaser", obj.getPurchaser());
		} else {
			st.setObject("purchaser", null);
}
if (obj.getSupplier() != Integer.MIN_VALUE) {
			st.setObject("supplier", obj.getSupplier());
		} else {
			st.setObject("supplier", null);
}
if (obj.getIdCurrency() != Integer.MIN_VALUE) {
			st.setObject("id_currency", obj.getIdCurrency());
		} else {
			st.setObject("id_currency", null);
}
if (obj.getIdProject() != Integer.MIN_VALUE) {
			st.setObject("id_project", obj.getIdProject());
		} else {
			st.setObject("id_project", null);
}
if (obj.getDiscount() != Double.MAX_VALUE) {
			st.setObject("discount", obj.getDiscount());
		} else {
			st.setObject("discount", null);
}
if (obj.getAdditionalCost() != Double.MAX_VALUE) {
			st.setObject("additional_cost", obj.getAdditionalCost());
		} else {
			st.setObject("additional_cost", null);
}
if (obj.getOrderTime() != null) {
			st.setObject("order_time", obj.getOrderTime());
		} else {
			st.setObject("order_time", null);
}
if (obj.getIdQuotation() != Integer.MIN_VALUE) {
			st.setObject("id_quotation", obj.getIdQuotation());
		} else {
			st.setObject("id_quotation", null);
}
if (obj.getIdQuotationItem() != Integer.MIN_VALUE) {
			st.setObject("id_quotation_item", obj.getIdQuotationItem());
		} else {
			st.setObject("id_quotation_item", null);
}
if (obj.getIdChecklist() != Integer.MIN_VALUE) {
			st.setObject("id_checklist", obj.getIdChecklist());
		} else {
			st.setObject("id_checklist", null);
}
if (obj.getIdTask() != Integer.MIN_VALUE) {
			st.setObject("id_task", obj.getIdTask());
		} else {
			st.setObject("id_task", null);
}
if (obj.getIdEvent() != Integer.MIN_VALUE) {
			st.setObject("id_event", obj.getIdEvent());
		} else {
			st.setObject("id_event", null);
}
if (obj.getPaymentDate() != null) {
			st.setObject("payment_date", obj.getPaymentDate());
		} else {
			st.setObject("payment_date", null);
}

	
		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genDeleteStatement(Connection dbConn, IModel query)
			throws SQLException {
		String sql = "select sp_purchase_delete(:id);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

        PurchaseModel obj = (PurchaseModel) query;

		// set primary keys
st.setObject("id", obj.getId());


		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genCountStatement(Connection dbConn,
			IModelExt input) throws SQLException {
		String sql = "SELECT sp_purchase_count(:id,:name,:code,:description,:plan_cost,:real_cost,:plan_order_timeFrom,:plan_order_timeTo,:plan_delivery_timeFrom,:plan_delivery_timeTo,:real_order_tateFrom,:real_order_tateTo,:real_delivery_timeFrom,:real_delivery_timeTo,:need_approval,:status,:is_draft,:notification,:createdFrom,:createdTo,:creator,:updatedFrom,:updatedTo,:updator,:deleted,:deletion_timeFrom,:deletion_timeTo,:remark,:purchaser,:supplier,:id_currency,:id_project,:discount,:additional_cost,:order_timeFrom,:order_timeTo,:id_quotation,:id_quotation_item,:id_checklist,:id_task,:id_event,:payment_dateFrom,:payment_dateTo)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(
				dbConn, sql);

		PurchaseModelExt obj = (PurchaseModelExt) input;

		// set parameters here
if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
            st.setObject("id", null);
}
if (obj.getName() != null) {
			st.setObject("name", obj.getName());
		} else {
            st.setObject("name", null);
}
if (obj.getCode() != null) {
			st.setObject("code", obj.getCode());
		} else {
            st.setObject("code", null);
}
if (obj.getDescription() != null) {
			st.setObject("description", obj.getDescription());
		} else {
            st.setObject("description", null);
}
if (obj.getPlanCost() != Double.MAX_VALUE) {
			st.setObject("plan_cost", obj.getPlanCost());
		} else {
            st.setObject("plan_cost", null);
}
if (obj.getRealCost() != Double.MAX_VALUE) {
			st.setObject("real_cost", obj.getRealCost());
		} else {
            st.setObject("real_cost", null);
}
if (obj.getPlanOrderTimeFrom() != null) {
			st.setObject("plan_order_timeFrom", obj.getPlanOrderTimeFrom());
		} else {
			st.setObject("plan_order_timeFrom", null);
		}
		if (obj.getPlanOrderTimeTo() != null) {
			st.setObject("plan_order_timeTo", obj.getPlanOrderTimeTo());
		} else {
            st.setObject("plan_order_timeTo", null);
}
if (obj.getPlanDeliveryTimeFrom() != null) {
			st.setObject("plan_delivery_timeFrom", obj.getPlanDeliveryTimeFrom());
		} else {
			st.setObject("plan_delivery_timeFrom", null);
		}
		if (obj.getPlanDeliveryTimeTo() != null) {
			st.setObject("plan_delivery_timeTo", obj.getPlanDeliveryTimeTo());
		} else {
            st.setObject("plan_delivery_timeTo", null);
}
if (obj.getRealOrderTateFrom() != null) {
			st.setObject("real_order_tateFrom", obj.getRealOrderTateFrom());
		} else {
			st.setObject("real_order_tateFrom", null);
		}
		if (obj.getRealOrderTateTo() != null) {
			st.setObject("real_order_tateTo", obj.getRealOrderTateTo());
		} else {
            st.setObject("real_order_tateTo", null);
}
if (obj.getRealDeliveryTimeFrom() != null) {
			st.setObject("real_delivery_timeFrom", obj.getRealDeliveryTimeFrom());
		} else {
			st.setObject("real_delivery_timeFrom", null);
		}
		if (obj.getRealDeliveryTimeTo() != null) {
			st.setObject("real_delivery_timeTo", obj.getRealDeliveryTimeTo());
		} else {
            st.setObject("real_delivery_timeTo", null);
}
		if (obj.getNeedApprovalSet()) {
			st.setObject("need_approval", obj.getNeedApproval());
		} else {
            st.setObject("need_approval", null);
}
if (obj.getStatus() != Integer.MIN_VALUE) {
			st.setObject("status", obj.getStatus());
		} else {
            st.setObject("status", null);
}
		if (obj.getIsDraftSet()) {
			st.setObject("is_draft", obj.getIsDraft());
		} else {
            st.setObject("is_draft", null);
}
if (obj.getNotification() != Integer.MIN_VALUE) {
			st.setObject("notification", obj.getNotification());
		} else {
            st.setObject("notification", null);
}
if (obj.getCreatedFrom() != null) {
			st.setObject("createdFrom", obj.getCreatedFrom());
		} else {
			st.setObject("createdFrom", null);
		}
		if (obj.getCreatedTo() != null) {
			st.setObject("createdTo", obj.getCreatedTo());
		} else {
            st.setObject("createdTo", null);
}
if (obj.getCreator() != Integer.MIN_VALUE) {
			st.setObject("creator", obj.getCreator());
		} else {
            st.setObject("creator", null);
}
if (obj.getUpdatedFrom() != null) {
			st.setObject("updatedFrom", obj.getUpdatedFrom());
		} else {
			st.setObject("updatedFrom", null);
		}
		if (obj.getUpdatedTo() != null) {
			st.setObject("updatedTo", obj.getUpdatedTo());
		} else {
            st.setObject("updatedTo", null);
}
if (obj.getUpdator() != Integer.MIN_VALUE) {
			st.setObject("updator", obj.getUpdator());
		} else {
            st.setObject("updator", null);
}
		if (obj.getDeletedSet()) {
			st.setObject("deleted", obj.getDeleted());
		} else {
            st.setObject("deleted", null);
}
if (obj.getDeletionTimeFrom() != null) {
			st.setObject("deletion_timeFrom", obj.getDeletionTimeFrom());
		} else {
			st.setObject("deletion_timeFrom", null);
		}
		if (obj.getDeletionTimeTo() != null) {
			st.setObject("deletion_timeTo", obj.getDeletionTimeTo());
		} else {
            st.setObject("deletion_timeTo", null);
}
if (obj.getRemark() != null) {
			st.setObject("remark", obj.getRemark());
		} else {
            st.setObject("remark", null);
}
if (obj.getPurchaser() != Integer.MIN_VALUE) {
			st.setObject("purchaser", obj.getPurchaser());
		} else {
            st.setObject("purchaser", null);
}
if (obj.getSupplier() != Integer.MIN_VALUE) {
			st.setObject("supplier", obj.getSupplier());
		} else {
            st.setObject("supplier", null);
}
if (obj.getIdCurrency() != Integer.MIN_VALUE) {
			st.setObject("id_currency", obj.getIdCurrency());
		} else {
            st.setObject("id_currency", null);
}
if (obj.getIdProject() != Integer.MIN_VALUE) {
			st.setObject("id_project", obj.getIdProject());
		} else {
            st.setObject("id_project", null);
}
if (obj.getDiscount() != Double.MAX_VALUE) {
			st.setObject("discount", obj.getDiscount());
		} else {
            st.setObject("discount", null);
}
if (obj.getAdditionalCost() != Double.MAX_VALUE) {
			st.setObject("additional_cost", obj.getAdditionalCost());
		} else {
            st.setObject("additional_cost", null);
}
if (obj.getOrderTimeFrom() != null) {
			st.setObject("order_timeFrom", obj.getOrderTimeFrom());
		} else {
			st.setObject("order_timeFrom", null);
		}
		if (obj.getOrderTimeTo() != null) {
			st.setObject("order_timeTo", obj.getOrderTimeTo());
		} else {
            st.setObject("order_timeTo", null);
}
if (obj.getIdQuotation() != Integer.MIN_VALUE) {
			st.setObject("id_quotation", obj.getIdQuotation());
		} else {
            st.setObject("id_quotation", null);
}
if (obj.getIdQuotationItem() != Integer.MIN_VALUE) {
			st.setObject("id_quotation_item", obj.getIdQuotationItem());
		} else {
            st.setObject("id_quotation_item", null);
}
if (obj.getIdChecklist() != Integer.MIN_VALUE) {
			st.setObject("id_checklist", obj.getIdChecklist());
		} else {
            st.setObject("id_checklist", null);
}
if (obj.getIdTask() != Integer.MIN_VALUE) {
			st.setObject("id_task", obj.getIdTask());
		} else {
            st.setObject("id_task", null);
}
if (obj.getIdEvent() != Integer.MIN_VALUE) {
			st.setObject("id_event", obj.getIdEvent());
		} else {
            st.setObject("id_event", null);
}
if (obj.getPaymentDateFrom() != null) {
			st.setObject("payment_dateFrom", obj.getPaymentDateFrom());
		} else {
			st.setObject("payment_dateFrom", null);
		}
		if (obj.getPaymentDateTo() != null) {
			st.setObject("payment_dateTo", obj.getPaymentDateTo());
		} else {
            st.setObject("payment_dateTo", null);
}

	
		return st.getPreparedStatement();
	}

/*
	private String fillHtmlTemplate(String template) {
		String html = template
				.replace(":id", Integer.toString(_id))
.replace(":name", HtmlUtil.genHtml(_name))
.replace(":code", HtmlUtil.genHtml(_code))
.replace(":description", HtmlUtil.genHtml(_description))
.replace(":plan_cost", StringUtil.toString(_plan_cost))
.replace(":real_cost", StringUtil.toString(_real_cost))
.replace(":plan_order_time", StringUtil.toDateTimeString(_plan_order_time))
.replace(":plan_delivery_time", StringUtil.toDateTimeString(_plan_delivery_time))
.replace(":real_order_tate", StringUtil.toDateTimeString(_real_order_tate))
.replace(":real_delivery_time", StringUtil.toDateTimeString(_real_delivery_time))
.replace(":need_approval", HtmlUtil.genHtml(_isneed_approvalSet, _need_approval))
.replace(":status", Integer.toString(_status))
.replace(":is_draft", HtmlUtil.genHtml(_isis_draftSet, _is_draft))
.replace(":notification", Integer.toString(_notification))
.replace(":created", StringUtil.toDateTimeString(_created))
.replace(":creator", Integer.toString(_creator))
.replace(":updated", StringUtil.toDateTimeString(_updated))
.replace(":updator", Integer.toString(_updator))
.replace(":deleted", HtmlUtil.genHtml(_isdeletedSet, _deleted))
.replace(":deletion_time", StringUtil.toDateTimeString(_deletion_time))
.replace(":remark", HtmlUtil.genHtml(_remark))
.replace(":purchaser", Integer.toString(_purchaser))
.replace(":supplier", Integer.toString(_supplier))
.replace(":id_currency", Integer.toString(_id_currency))
.replace(":id_project", Integer.toString(_id_project))
.replace(":discount", StringUtil.toString(_discount))
.replace(":additional_cost", StringUtil.toString(_additional_cost))
.replace(":order_time", StringUtil.toDateTimeString(_order_time))
.replace(":id_quotation", Integer.toString(_id_quotation))
.replace(":id_quotation_item", Integer.toString(_id_quotation_item))
.replace(":id_checklist", Integer.toString(_id_checklist))
.replace(":id_task", Integer.toString(_id_task))
.replace(":id_event", Integer.toString(_id_event))
.replace(":payment_date", StringUtil.toDateTimeString(_payment_date))

				;
		return html;
	}
*/

/*
	private String fillHtmlTemplate(String template) {
		String html = template
				.replace(":Id", Integer.toString(_Id))
.replace(":Name", HtmlUtil.genHtml(_Name))
.replace(":Code", HtmlUtil.genHtml(_Code))
.replace(":Description", HtmlUtil.genHtml(_Description))
.replace(":PlanCost", StringUtil.toString(_PlanCost))
.replace(":RealCost", StringUtil.toString(_RealCost))
.replace(":PlanOrderTime", StringUtil.toDateTimeString(_PlanOrderTime))
.replace(":PlanDeliveryTime", StringUtil.toDateTimeString(_PlanDeliveryTime))
.replace(":RealOrderTate", StringUtil.toDateTimeString(_RealOrderTate))
.replace(":RealDeliveryTime", StringUtil.toDateTimeString(_RealDeliveryTime))
.replace(":NeedApproval", HtmlUtil.genHtml(_isNeedApprovalSet, _NeedApproval))
.replace(":Status", Integer.toString(_Status))
.replace(":IsDraft", HtmlUtil.genHtml(_isIsDraftSet, _IsDraft))
.replace(":Notification", Integer.toString(_Notification))
.replace(":Created", StringUtil.toDateTimeString(_Created))
.replace(":Creator", Integer.toString(_Creator))
.replace(":Updated", StringUtil.toDateTimeString(_Updated))
.replace(":Updator", Integer.toString(_Updator))
.replace(":Deleted", HtmlUtil.genHtml(_isDeletedSet, _Deleted))
.replace(":DeletionTime", StringUtil.toDateTimeString(_DeletionTime))
.replace(":Remark", HtmlUtil.genHtml(_Remark))
.replace(":Purchaser", Integer.toString(_Purchaser))
.replace(":Supplier", Integer.toString(_Supplier))
.replace(":IdCurrency", Integer.toString(_IdCurrency))
.replace(":IdProject", Integer.toString(_IdProject))
.replace(":Discount", StringUtil.toString(_Discount))
.replace(":AdditionalCost", StringUtil.toString(_AdditionalCost))
.replace(":OrderTime", StringUtil.toDateTimeString(_OrderTime))
.replace(":IdQuotation", Integer.toString(_IdQuotation))
.replace(":IdQuotationItem", Integer.toString(_IdQuotationItem))
.replace(":IdChecklist", Integer.toString(_IdChecklist))
.replace(":IdTask", Integer.toString(_IdTask))
.replace(":IdEvent", Integer.toString(_IdEvent))
.replace(":PaymentDate", StringUtil.toDateTimeString(_PaymentDate))

				;
		return html;
	}
*/
// ADDED CODE
	private List<PurchaseItemModel> _items = new ArrayList<PurchaseItemModel>();

	public List<PurchaseItemModel> getItems() {
		return _items;
	}

	public void setItems(List<PurchaseItemModel> value) {
		_items = value;
	}

	public int getItemCount() {
		return _itemCount;
	}

	public void setItemCount(int itemCount) {
		this._itemCount = itemCount;
	}

	private int _itemCount = 0;
	
	// Dat added getPurchaseCost methods
		public double getPurchaseCost() throws Exception {
			_sum_item_cost=0;
			List<PurchaseItemModel> list=CacheUtil.getPurchaseItemList(_id);
				for(int i=0;i<list.size();i++)
				{
					_sum_item_cost += list.get(i).getItemCost();
				}
			_purchase_cost=_sum_item_cost - _discount + _additional_cost;
			return _purchase_cost;
		}
		
		public String getPurchaseCostToString() throws Exception {
			getPurchaseCost();
			if (_purchase_cost == Double.MIN_VALUE || _purchase_cost == Double.MAX_VALUE) {
				return "";
			} else {
				return StringUtil.toString(_purchase_cost);
			}
		}

}